﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KelasAdd : Form
    {
        List<int> groupID = new List<int>();

        public KelasAdd()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(dgvMurid);
        }

        private void form_load(object sender, EventArgs e)
        {
            DataTable dt = Database.getInstance().getGroups();

            foreach (DataRow row in dt.Rows)
            {
                // Load group
                groupID.Add(int.Parse(row[0].ToString()));
                cmbGrup.Items.Add(row[1].ToString());

                // Load School Session
                DateTime date = DateTime.Now;
                if (date.Month < 7)
                {
                    num1.Value = date.Year - 1;
                    num2.Value = num1.Value + 1;
                }
            }

            loadStudent();
        }

        private void loadStudent()
        {
            string sql = "SELECT s.stu_id AS [No. Induk], s.stu_name AS [Nama Murid], p.par_name AS [Nama Orangtua], s.stu_regsession AS [Thn. Pendaftaran] " +
                         "FROM Student AS s INNER JOIN Parent AS p ON s.par_id = p.par_id LEFT OUTER JOIN Enrollment AS e ON s.stu_id = e.stu_id " +
                         "WHERE e.stu_id IS NULL";
            DataTable dt = Database.getInstance().query(sql);

            dgvMurid.DataSource = dt;
        }

        private void num1_valuechanged(object sender, EventArgs e)
        {
            num2.Value = num1.Value + 1;
        }

        private void num2_valuechanged(object sender, EventArgs e)
        {
            num1.Value = num2.Value - 1;
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMurid.CurrentRow.Index < 0) throw new Exception("Mohon pilih salah satu murid");
                if (cmbGrup.SelectedIndex < 0) throw new Exception("Mohon pilih salah satu kelas/grup");

                DataTable dt = Database.getInstance().query("SELECT TOP 1 * FROM Enrollment");
                DataRow row = dt.NewRow();
                row[0] = dgvMurid.CurrentRow.Cells[0].Value.ToString();
                row[1] = groupID[cmbGrup.SelectedIndex];
                row[2] = num1.Value + " - " + num2.Value;
                if (txtUrut.Text.Length == 0) row[3] = DBNull.Value;
                else row[3] = txtUrut.Text;
                dt.Rows.Add(row);

                Database.getInstance().insert(dt);

                string message = String.Format("Murid bernama {0} telah dimasukkan ke dalam kelas/grup {1}", dgvMurid.CurrentRow.Cells[1].Value.ToString(), cmbGrup.SelectedItem.ToString());
                MessageBox.Show(message, "Sukses!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Gagal!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
